<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>面向对象基本语法 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter04.html" />
    
    
    <link rel="prev" href="./chapter02.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="3"
        data-chapter-title="面向对象基本语法"
        data-filepath="chapter03.md"
        data-basepath="."
        data-revision="Wed Feb 19 2020 00:43:01 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        类和对象
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        面向对象基本语法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        魔法方法1
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        魔法方法2
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        内置属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter07.html">
            
                
                    <a href="./chapter07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        类属性和对象属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter08.html">
            
                
                    <a href="./chapter08.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        私有属性和方法
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x57FA;&#x672C;&#x8BED;&#x6CD5;">&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x57FA;&#x672C;&#x8BED;&#x6CD5;</h2>
<p>&#x5728;Python&#x4E2D;&#xFF0C;&#x5BF9;&#x8C61;&#x51E0;&#x4E4E;&#x662F;&#x65E0;&#x5904;&#x4E0D;&#x5728;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>dir</code>&#x5185;&#x7F6E;&#x51FD;&#x6570;&#x6765;&#x67E5;&#x770B;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x91CC;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<h3 id="&#x5B9A;&#x4E49;&#x7B80;&#x5355;&#x7684;&#x7C7B;&#xFF08;&#x53EA;&#x5305;&#x542B;&#x65B9;&#x6CD5;&#xFF09;">&#x5B9A;&#x4E49;&#x7B80;&#x5355;&#x7684;&#x7C7B;&#xFF08;&#x53EA;&#x5305;&#x542B;&#x65B9;&#x6CD5;&#xFF09;</h3>
<blockquote>
<p>&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x662F;&#x66F4;&#x5927;&#x7684;&#x5C01;&#x88C5;&#xFF0C;&#x5728;&#x4E00;&#x4E2A;&#x7C7B;&#x4E2D;&#x5C01;&#x88C5;&#x591A;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x8FD9;&#x6837;&#x901A;&#x8FC7;&#x8FD9;&#x4E2A;&#x7C7B;&#x521B;&#x5EFA;&#x51FA;&#x6765;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8C03;&#x7528;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x4E86;&#xFF01;</p>
</blockquote>
<h4 id="&#x5B9A;&#x4E49;&#x7C7B;">&#x5B9A;&#x4E49;&#x7C7B;</h4>
<p>&#x5728;Python&#x4E2D;&#x8981;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x53EA;&#x5305;&#x542B;&#x65B9;&#x6CD5;&#x7684;&#x7C7B;&#xFF0C;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> &#x7C7B;&#x540D;:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> &#x65B9;&#x6CD5;1<span class="hljs-params">(self,&#x53C2;&#x6570;&#x5217;&#x8868;)</span>:</span>
        <span class="hljs-keyword">pass</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> &#x65B9;&#x6CD5;2<span class="hljs-params">(self,&#x53C2;&#x6570;&#x5217;&#x8868;)</span>:</span>
        <span class="hljs-keyword">pass</span>
</code></pre>
<blockquote>
<ol>
<li>&#x65B9;&#x6CD5;&#x7684;&#x5B9A;&#x4E49;&#x683C;&#x5F0F;&#x548C;&#x4E4B;&#x524D;&#x5B66;&#x4E60;&#x8FC7;&#x7684;&#x51FD;&#x6570;&#x4E00;&#x6837;</li>
<li>&#x65B9;&#x6CD5;&#x91CC;&#x7684;<span style="color:red;font-weight:700">&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x5FC5;&#x987B;&#x662F;self</span>&#xFF0C;&#x5927;&#x5BB6;&#x6682;&#x65F6;&#x5148;&#x8BB0;&#x4F4F;&#xFF0C;&#x7A0D;&#x540E;&#x4ECB;&#x7ECD; self.</li>
<li>&#x7C7B;&#x540D;&#x8981;&#x9075;&#x5B88;&#x5927;&#x9A7C;&#x5CF0;&#x547D;&#x540D;&#x6CD5;&#x3002;</li>
</ol>
</blockquote>
<h4 id="&#x521B;&#x5EFA;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;">&#x521B;&#x5EFA;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;</h4>
<p>&#x5F53;&#x4E00;&#x4E2A;&#x7C7B;&#x5B9A;&#x4E49;&#x5B8C;&#x6210;&#x4E4B;&#x540E;&#xFF0C;&#x8981;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x7C7B;&#x6765;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#xFF0C;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">&#x5BF9;&#x8C61;&#x53D8;&#x91CF;&#x540D; = &#x7C7B;&#x540D;()
</code></pre>
<h4 id="&#x7B2C;&#x4E00;&#x4E2A;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x4EE3;&#x7801;">&#x7B2C;&#x4E00;&#x4E2A;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x4EE3;&#x7801;</h4>
<p>&#x9700;&#x6C42;</p>
<ul>
<li>&#x5C0F;&#x732B; &#x7231; &#x5403; &#x9C7C;&#xFF0C;&#x5C0F;&#x732B; &#x8981; &#x559D; &#x6C34;</li>
</ul>
<p>&#x5206;&#x6790;</p>
<ul>
<li>&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x732B;&#x7C7B; Cat</li>
<li>&#x5B9A;&#x4E49;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5; eat &#x548C; drink</li>
<li>&#x6309;&#x7167;&#x9700;&#x6C42; &#x2014;&#x2014; &#x4E0D;&#x9700;&#x8981;&#x5B9A;&#x4E49;&#x5C5E;&#x6027;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x8FD9;&#x662F;&#x4E2A;&#x732B;&#x7C7B;&quot;&quot;&quot;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">eat</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&quot;&#x5C0F;&#x732B;&#x5728;&#x5403;&#x4E1C;&#x897F;&quot;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">drink</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&quot;&#x5C0F;&#x732B;&#x5728;&#x559D;&#x6C34;&quot;</span>)

tom = Cat()  <span class="hljs-comment"># &#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;Cat&#x5BF9;&#x8C61;</span>
tom.eat()
tom.drink()

hello_kitty = Cat()  <span class="hljs-comment"># &#x53C8;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;Cat&#x5BF9;&#x8C61;</span>
hello_kitty.eat()
hello_kitty.drink()
</code></pre>
<p>&#x601D;&#x8003;&#xFF1A;<code>tom</code> &#x548C; <code>hello_kitty</code> &#x662F;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x5417;&#xFF1F;</p>
<h3 id="self&#x7684;&#x4F7F;&#x7528;">self&#x7684;&#x4F7F;&#x7528;</h3>
<h4 id="&#x7ED9;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;">&#x7ED9;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;</h4>
<p>python&#x652F;&#x6301;&#x52A8;&#x6001;&#x5C5E;&#x6027;&#xFF0C;&#x5F53;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x521B;&#x5EFA;&#x597D;&#x4E86;&#x4EE5;&#x540E;&#xFF0C;&#x76F4;&#x63A5;&#x4F7F;&#x7528; <code>&#x5BF9;&#x8C61;.&#x5C5E;&#x6027;&#x540D; = &#x5C5E;&#x6027;&#x503C;</code> &#x5C31;&#x53EF;&#x4EE5;&#x5F88;&#x65B9;&#x4FBF;&#x7684;&#x7ED9;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#x3002;</p>
<pre><code class="lang-python">tom = Cat()
tom.name = <span class="hljs-string">&apos;Tom&apos;</span>  <span class="hljs-comment"># &#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7ED9; tom &#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A; name &#x5C5E;&#x6027;</span>
</code></pre>
<p>&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#x5F88;&#x65B9;&#x4FBF;&#xFF0C;&#x4F46;&#x662F;&#xFF0C;&#x4E0D;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x7ED9;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;&#x3002;</p>
<h4 id="self&#x7684;&#x6982;&#x5FF5;">self&#x7684;&#x6982;&#x5FF5;</h4>
<p>&#x54EA;&#x4E2A;&#x5BF9;&#x8C61;&#x8C03;&#x7528;&#x4E86;&#x65B9;&#x6CD5;&#xFF0C;&#x65B9;&#x6CD5;&#x91CC;&#x7684;<code>self</code>&#x6307;&#x7684;&#x5C31;&#x662F;&#x8C01;&#x3002;
&#x901A;&#x8FC7; <code>self.&#x5C5E;&#x6027;&#x540D;</code> &#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5230;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#xFF1B;&#x901A;&#x8FC7; <code>self.&#x65B9;&#x6CD5;&#x540D;()</code> &#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">eat</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&quot;%s&#x7231;&#x5403;&#x9C7C;&quot;</span> %self.name)

tom = Cat()
tom.name = <span class="hljs-string">&apos;Tom&apos;</span>  <span class="hljs-comment"># &#x7ED9; tom &#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;name&#x5C5E;&#x6027;</span>
tom.eat()  <span class="hljs-comment"># Tom&#x7231;&#x5403;&#x9C7C;</span>

lazy_cat = Cat()
lazy_cat.name = <span class="hljs-string">&quot;&#x5927;&#x61D2;&#x732B;&quot;</span>
lazy_cat.eat() <span class="hljs-comment"># &#x5927;&#x61D2;&#x732B;&#x7231;&#x5403;&#x9C7C;</span>
</code></pre>
<h4 id="&#x76F4;&#x63A5;&#x7ED9;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;&#x7684;&#x7F3A;&#x70B9;">&#x76F4;&#x63A5;&#x7ED9;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;&#x7684;&#x7F3A;&#x70B9;</h4>
<p>&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x662F;&#x5148;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x7ED9;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0; <code>name</code> &#x5C5E;&#x6027;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x6837;&#x505A;&#x4F1A;&#x6709;&#x95EE;&#x9898;&#x3002;</p>
<pre><code class="lang-python">tom = Cat()
tom.eat()
tom.anme = <span class="hljs-string">&quot;Tom&quot;</span>
</code></pre>
<p>&#x7A0B;&#x5E8F;&#x8FD0;&#x884C;&#x65F6;&#x4F1A;&#x62A5;&#x9519;&#xFF1A;</p>
<pre><code class="lang-python">AttributeError: <span class="hljs-string">&apos;Cat&apos;</span> object has no attribute <span class="hljs-string">&apos;name&apos;</span>
&#x9519;&#x8BEF;&#x63D0;&#x793A;&#xFF1A;<span class="hljs-string">&apos;Cat&apos;</span>&#x5BF9;&#x8C61;&#x6CA1;&#x6709; <span class="hljs-string">&apos;name&apos;</span> &#x5C5E;&#x6027;
</code></pre>
<p>&#x5728;&#x65E5;&#x5E38;&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x4E0D;&#x63A8;&#x8350;&#x5728;&#x7C7B;&#x7684;&#x5916;&#x90E8;&#x76F4;&#x63A5;&#x7ED9;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x3002;&#x5BF9;&#x8C61;&#x5E94;&#x8BE5;&#x5177;&#x6709;&#x54EA;&#x4E9B;&#x5C5E;&#x6027;&#xFF0C;&#x6211;&#x4EEC;&#x5E94;&#x8BE5;&#x5C01;&#x88C5;&#x5728;&#x7C7B;&#x7684;&#x5185;&#x90E8;&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter02.html" class="navigation navigation-prev " aria-label="Previous page: 类和对象"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter04.html" class="navigation navigation-next " aria-label="Next page: 魔法方法1"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
